<template>
  <basic-container>
    <div class="container-body">
      <el-form :inline="true" :model="query" class="demo-form-inline">
        <el-row>
          <el-form-item label="项目编号:">
            <el-input v-model="query.projectCode" clearable placeholder="项目编号" size="small"></el-input>
          </el-form-item>
          <el-form-item label="项目名称:">
            <el-input v-model="query.projectName" clearable placeholder="项目名称"></el-input>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item>
            <el-button size="small" type="primary" icon="el-icon-search" @click="onSearch" :loading="loading">搜索
            </el-button>
            <el-button size="small" icon="el-icon-delete" @click="searchReset" :loading="loading">清空</el-button>
          </el-form-item>
        </el-row>
      </el-form>
      <el-table v-loading="loading" :data="data" border
                header-row-class-name="container-table-header"
                highlight-current-row @cell-click="handleCellClick"
                row-key="projectId" size="small" stripe
                style="width: 100%;margin-bottom: 20px;">
        <el-table-column label="项目编号" prop="projectCode">
        </el-table-column>
        <el-table-column label="项目名称" prop="projectName">
        </el-table-column>
        <el-table-column label="被审计单位" prop="auditedCompanyName">
        </el-table-column>
        <el-table-column label="被审计个人" prop="auditedUserNames">
        </el-table-column>
        <el-table-column label="审计类型" prop="effectiveDate" >
          <template slot-scope="scope">
            <DictTag v-if="scope.row.auditType !== 4" :options="dict.type.audit_type" :value="scope.row.auditType"></DictTag>
            <span v-if="scope.row.auditType === 4">工程审计</span>
          </template>
        </el-table-column>
        <el-table-column label="项目进度" prop="projectProgress">
          <template slot-scope="scope">
            <DictTag :options="dict.type.project_progress" :value="scope.row.projectProgress"></DictTag>
          </template>
        </el-table-column>
        <el-table-column
          label="操作" width="170">
          <template slot-scope="scope">
            <el-button v-if="permissions.project_archive_get" type="text" size="small" icon="el-icon-view" @click="showViewBox(scope.row)">查看</el-button>
            <el-button v-if="permissions.project_archive && scope.row.projectProgress !== 6" type="text" size="small" icon="el-icon-edit" @click="handleArchive(scope.row)">项目归档打包</el-button>
            <el-button v-if="permissions.project_archive_gen && scope.row.projectProgress === 6" type="text" size="small" icon="el-icon-edit" @click="downloadArchiveZip(scope.row)">归档资料下载</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="container-page">
        <el-pagination
          @size-change="sizeChange"
          @current-change="currentChange"
          :current-page="page.current"
          :page-sizes="[10, 20, 30, 40, 50, 100]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total">
        </el-pagination>
      </div>
    </div>

    <el-dialog top="5vh" :title="formTitle" :visible.sync="viewBox"
               :append-to-body="true" :before-close="closeViewBox" width="1200px">
      <component :is="auditView" :params="form" :management="isManagement"></component>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeViewBox" size="small" icon="el-icon-close">关闭</el-button>
      </span>
    </el-dialog>

  </basic-container>
</template>

<script>
import {doArchive, fetchList} from "@/api/adis/archive/archive";
import {mapGetters} from "vuex";
import ProjectAuditView from "@/components/actForm/ProjectAuditView";
import {getProject} from "@/api/adis/project/project";

export default {
  components: {
    ProjectAuditView
  },
  dictTypes: ['audit_type', 'project_progress'],
  data() {
    return {
      importBox: false,
      isEdit: false,
      isManagement: true,
      viewBox: false,
      formBox: false,
      formTitle: '',
      auditView: '',
      loading: false,
      query: {},
      form: {},
      page: {
        size: 10,
        current: 1,
        total: 0
      },
      data: [],
      showUploadButton: true,
      fileList: [], // 封面图
      fileListVideo: [], // 视频
      editorOption: {
        placeholder: "",
      },
      auditBox: false,
      buyBox: false,
    }
  },
  computed: {
    ...mapGetters(["permissions"])
  },
  created() {
    // categoryOptions
    this.onLoad(this.page);
  },
  methods: {
    handleCellClick(row, column) {
      if (row && column.label !== "操作"){
        this.showViewBox(row)
      }
    },

    submit() {
      this.$refs['optForm'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.fileList && this.fileList.map((file) => {
            this.form.cover = file.response ? file.response.data.url : file.url
          });
          this.fileListVideo && this.fileListVideo.map((file) => {
            this.form.videoUrl = file.response ? file.response.data.url : file.url
          });
          submit(this.form).then(() => {
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.resetForm();
            this.onLoad(this.page);
          }, () => {
            this.loading = false;
          });
        } else {
          return false;
        }
      });
    },
    showViewBox(row) {
      this.auditView = "ProjectAuditView"
      getProject(row.projectId).then(response => {
        this.form = response.data.data
        this.formTitle = row.projectName;
        this.viewBox = true;
      })
    },
    closeViewBox() {
      this.viewBox = false;
    },

    handleArchive(row) {
      this.$confirm("确定将选择的项目归档?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        return doArchive(row.projectId);
      })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    resetForm() {
      this.formBox = false;
      this.isEdit = false;
      this.form = {
        agencyType: 1, // 景点
        tags: []
      };
      this.formTitle = '';
      this.$refs['optForm'] && this.$refs['optForm'].resetFields();
      this.viewBox = false;
      this.fileList = [];
      this.fileListVideo = [];
      this.auditBox = false;

      this.customerBox = false;
      this.customerList = [];

      this.commentBox = false;
      this.commentList = [];
    },
    onSearch() {
      this.page.current = 1;
      this.onLoad(this.page);
    },
    searchReset() {
      this.query = {};
      this.page.current = 1;
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.current = 1;
      this.onLoad(this.page, params);
      done();
    },
    currentChange(current) {
      this.page.current = current;
      this.onLoad(this.page);
    },
    sizeChange(size) {
      this.page.size = size;
      this.onLoad(this.page);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      fetchList(Object.assign(params, this.query, page)).then(res => {
        this.loading = false;
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
      }, () => {
        this.loading = false;
      });
    },
    handleRemove(file, fileList) {
      this.fileList = fileList;
      this.showUploadButton = true
    },
    handleSuccess(response, file, fileList) {
      this.fileList = fileList;
      if (this.fileList.length >= 1) {
        this.showUploadButton = false
      }
    },
    handlePreview(file) {
      window.open(file.url ? file.url : file.response.data.url)
    },
    beforeRemove(file) {
      return this.$confirm(`确定移除 ${file.name}？`);
    },

    downloadArchiveZip: function(row) {
      this.downBlobFile(
        "/exam/project/archive/downloadArchiveZip/"+ row.projectId,
        {},
        row.projectName + ".zip"
      )
    },
  }
};
</script>

<style>
</style>
